package com.ushine.web.communication.threads;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.springframework.stereotype.Component;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author: yls
 * @Date 2022/5/11 11:21
 * @Description: 通信线程池
 * @Version 1.0
 **/
@Component
public class CommunicationThreads {

    public static ThreadPoolExecutor EXECUTOR = null;

    static {
        EXECUTOR = initThreads();
    }

    /**
     * 功能描述:
     * 〈〉
     *
     * @param
     * @return : void
     * @author : yls
     * @date : 2022/5/11 11:29
     */
    private static ThreadPoolExecutor initThreads() {
        // 使用 ThreadFactoryBuilder 创建自定义线程名称的 ThreadFactory
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
                .setNameFormat("hyn-demo-pool-%d").build();
        // 创建线程池，其中任务队列需要结合实际情况设置合理的容量
        ThreadPoolExecutor executor = new ThreadPoolExecutor(1000,
                1000,
                0L,
                TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(1024),
                namedThreadFactory,
                new ThreadPoolExecutor.AbortPolicy());
        return executor;
    }
}
